package com.xxx.yuedong.community.service.impl;

import cn.hutool.core.map.MapUtil;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.common.exception.YuedongException;
import com.xxx.yuedong.community.db.dao.DynamicDao;
import com.xxx.yuedong.community.pojo.Dynamic;
import com.xxx.yuedong.community.service.DynamicService;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    private DynamicDao dynamicDao;

    @Override
    public int insert(Dynamic dynamic) {
        int rows = dynamicDao.insert(dynamic);
        return rows;
    }

    @Override
    public int update(Dynamic dynamic) {
        int rows = dynamicDao.update(dynamic);
        if (rows == 0){
            throw new YuedongException(HttpStatus.SC_FORBIDDEN,"不是本人的动态不能编辑");
        }
        return rows;
    }

    @Override
    public int deleteDynamicById(HashMap param) {
        int rows = dynamicDao.deleteDynamicById(param);
        if (rows == 0){
            throw new YuedongException(HttpStatus.SC_BAD_REQUEST,"未能成功删除动态");
        }
        return rows;
    }

    @Override
    public HashMap searchDynamicDetailById(Long id) {
        HashMap map = dynamicDao.searchDynamicDetailById(id);
        return map;
    }

    @Override
    public R searchDynamicByPage(HashMap param) {
        Integer page = MapUtil.getInt(param, "page");
        Integer pageSize = MapUtil.getInt(param, "pageSize");
        int start = (page - 1) * pageSize;
        param.put("start",start);
        param.put("length",pageSize);
        ArrayList<HashMap> list = dynamicDao.searchDynamicByPage(param);
        Integer totalCount = dynamicDao.searchDynamicCount(param);
        return R.ok().put("list",list).put("totalCount",totalCount);
    }
}
